<?php

namespace app\system\controller;

use app\common\model\ShopAttrModel;
use app\common\model\ShopExtendModel;
use app\common\model\ShopGoodsBrandModel;
use app\common\model\ShopGoodsDataModel;
use app\common\model\ShopGoodsModel;
use app\common\model\ShopGoodsSkuModel;
use app\common\model\SiteModel;
use app\common\model\UserModel;
use org\util\TreeUtil;
use think\Db;
use think\Request;

class ShopGoodsController extends BaseController
{

    private $goodsCategoryCache;
    private $goodsModel;
    private $goodsDataModel;
    private $goodsSkuModel;
    private $extendModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->goodsModel         = new ShopGoodsModel();
        $this->goodsDataModel     = new ShopGoodsDataModel();
        $this->extendModel        = new ShopExtendModel();
        $this->goodsSkuModel      = new ShopGoodsSkuModel();
        $this->goodsCategoryCache = cache('goods_category');
    }

    /**
     * 首页
     */
    public function index()
    {
        $map                = [];
        $query              = $this->request->request();
        $goodsName = $this->request->param('goods_name');
        $siteName = $this->request->param('site_name');
        $is_on_sale = $this->request->param('is_on_sale',-2);
        if (!empty($goodsName)) {
            $map['goods_name']   = ['like', '%' . $goodsName . '%'];
        }
        if (!empty($siteName)) {
            $map['site_name']   = ['like', '%' . $siteName . '%'];
        }
        if($is_on_sale>-2){
            $map['is_on_sale'] = $is_on_sale;
        }
        $cid = $this->request->param('cid');
        if ($cid > 0) {
            $cids         = array_keys(\org\util\TreeUtil::channelList($this->goodsCategoryCache, $cid, "&nbsp;", 'cid', 'pid'));
            $cids[]       = $cid; //加入当前栏目
            $map['cid']   = ['in', $cids];
        }
        $list = $this->goodsModel->where($map)->order('goods_sort desc')->paginate(12, false, [
            'query' => $query,
        ]);
        //echo $this->goodsModel->getLastSql();
        cookie('__GOODSURL__', get_current_url());
        $this->assign('category', $this->goodsCategoryCache);
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }

    /**
     * 违规下架
     */
    public function soldOut(){
        $goodsId = $this->request->param('goods_id/d');
        $saleRemark = $this->request->param('sale_remark');
        $this->goodsModel->where('goods_id',$goodsId)->setField(['is_on_sale'=>10,'sale_remark'=>$saleRemark]);
        $this->success('操作成功！');
    }

    public function soldUp(){
        $goodsId = $this->request->param('goods_id/d');
        $this->goodsModel->where('goods_id',$goodsId)->setField('is_on_sale',1);
        $this->success('操作成功！');
    }
    /**
     * 删除
     */
    public function del()
    {
        $goodsId = $this->request->param('goods_id/d');
        $this->goodsModel->delData($goodsId);
        $this->success('删除成功！');
    }

    /**
     *排序
     */
    public function updateOrder()
    {
        $listOrder = $this->request->param('list_order/a');
        $dataList=[];
        foreach ($listOrder as $goods_id => $goods_sort) {
            $goodsId    = intval($goods_id);
            $dataList[] = [
                "goods_id" => $goodsId,
                "goods_sort" => intval($goods_sort['goods_sort']),
                "content_tpl"=>$goods_sort['content_tpl'],
                "goods_sale_number"=>intval($goods_sort['goods_sale_number'])];
        }
        $this->goodsModel->saveAll($dataList);
        $this->success('保存成功！');
    }

}
